package com.cracker.websocket.server.service.impl;

import com.cracker.websocket.server.constant.CmdConstant;
import com.cracker.websocket.server.dto.CmdDto;
import com.cracker.websocket.server.dto.message.ChatMessageDto;
import com.cracker.websocket.server.service.ChatService;
import com.cracker.websocket.server.service.MessageHandler;
import com.cracker.websocket.server.service.MessageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.websocket.Session;
import java.io.IOException;


/**
 * 单聊消息
 */
@Component
@Slf4j
public class MessageHandlerImplChat implements MessageHandler {
    @Autowired
    private ChatService chatService;
    @Autowired
    private MessageService messageService;

    @Override
    public void doCmd(Session session, CmdDto cmdDto) throws IOException {
        switch (cmdDto.getCmd()) {
            case CmdConstant.CHAT_MSG:
                ChatMessageDto messageDto = messageService.decode(cmdDto.getData(), ChatMessageDto.class);
                chatService.sendMsg(session, cmdDto.getTargetId(), messageDto);
                break;
        }
    }
}
